package com.wtf.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wtf.generator.common.utils.PageUtils;
import com.wtf.generator.entity.WtfTemplateConfig;
import com.wtf.generator.mapper.WtfTemplateConfigMapper;
import com.wtf.generator.model.dto.*;
import com.wtf.generator.model.vo.TemplateConfigVo;
import com.wtf.generator.service.WtfTemplateConfigService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * date: 2022/6/18 10:59
 * @author linhuanyou
 * @email 496558736@qq.com
 * @version 1.0
 */
@Service("wtfTemplateConfigService")
public class WtfTemplateConfigServiceImpl extends ServiceImpl<WtfTemplateConfigMapper, WtfTemplateConfig> implements WtfTemplateConfigService {


    @Value("${spring.datasource.dynamic.primary}")
    private String primaryDs;

    @Override
    public int add(TemplateAddDTO templateAddDTO) {
        WtfTemplateConfig wtfTemplateConfig = new WtfTemplateConfig();
        BeanUtils.copyProperties(templateAddDTO,wtfTemplateConfig);
        int insert = this.getBaseMapper().insert(wtfTemplateConfig);
        return insert;
    }

    @Override
    public PageUtils page(TemplatePageDTO templateListDTO) {
        Page<?> page = PageHelper.startPage(templateListDTO.getPage(), templateListDTO.getLimit());
        LambdaQueryWrapper<WtfTemplateConfig> wtfTemplateConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank( templateListDTO.getDs())){
            wtfTemplateConfigLambdaQueryWrapper.eq(WtfTemplateConfig::getDs,templateListDTO.getDs());
        }
        if(StringUtils.isNotBlank( templateListDTO.getAuthor())){
            wtfTemplateConfigLambdaQueryWrapper.like(WtfTemplateConfig::getAuthor,templateListDTO.getAuthor());
        }
        if(templateListDTO.getId() != null){
            wtfTemplateConfigLambdaQueryWrapper.eq(WtfTemplateConfig::getId,templateListDTO.getId());
        }
        wtfTemplateConfigLambdaQueryWrapper.orderByAsc(WtfTemplateConfig::getId);
        List<TemplateConfigVo> list = this.list(wtfTemplateConfigLambdaQueryWrapper).stream().map(e->{
            TemplateConfigVo templateConfigVo = new TemplateConfigVo();
            BeanUtils.copyProperties(e,templateConfigVo);
            return templateConfigVo;
        }).collect(Collectors.toList());;
        int total = (int) page.getTotal();
        return new PageUtils(list, total, templateListDTO.getLimit(),templateListDTO.getPage());
    }

    @Override
    public int update(TemplateUpdDTO templateUpdDTO) {
        WtfTemplateConfig wtfTemplateConfig = new WtfTemplateConfig();
        BeanUtils.copyProperties(templateUpdDTO,wtfTemplateConfig);
        int update = this.getBaseMapper().updateById(wtfTemplateConfig);
        return update;
    }

    @Override
    public int remove(TemplateDelDTO templateDelDTO) {
        return this.getBaseMapper().deleteBatchIds(templateDelDTO.getIds());
    }

    @Override
    public List<TemplateConfigVo> list(TemplateListDTO templateListDTO) {
        LambdaQueryWrapper<WtfTemplateConfig> wtfTemplateConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wtfTemplateConfigLambdaQueryWrapper.eq(WtfTemplateConfig::getDs,templateListDTO.getDs());
        List<TemplateConfigVo> list = this.list(wtfTemplateConfigLambdaQueryWrapper).stream().map(e->{
            TemplateConfigVo templateConfigVo = new TemplateConfigVo();
            BeanUtils.copyProperties(e,templateConfigVo);
            return templateConfigVo;
        }).collect(Collectors.toList());
        return list;
    }
}